﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mongaba.Core.ServiceContracts;
using System.Management;
using System.Diagnostics;
using System.Threading;

namespace Mongaba.Core
{
    [Serializable]
    internal class ProcessScore
    {

        float _score = 0;
        float _totalMemory = 0;
        float _freeMemory = 0;
        float _freeCPU = 0;
        float _totalCPU = 0;


        #region IProcessScore Members

        public float getTotalMemory()
        {
            return _totalMemory;
        }

        public float getFreeMemory()
        {
            return _freeMemory;
        }

        public float getTotalCPU()
        {
            return _totalCPU = 0;
        }

        public float getFreeCPU()
        {
            return _freeCPU;
        }

        public float getScore()
        {
            return _score;
        }

        public void refreshScore()
        {
            Thread th = new Thread(new ThreadStart(delegate
            {
                try
                {
                    refreshFreeMemory();
                    refreshTotalMemory();
                    refreshFreeCPU();
                    refreshTotalCPU();
                    _score = _freeCPU + _freeMemory + _totalCPU + _totalMemory;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
            }));
            th.Name = "Calculete Score";
            th.IsBackground = true;
            th.Start();

        }

        void refreshTotalMemory()
        {
            try
            {
                SelectQuery sQQuery = new SelectQuery("CIM_OperatingSystem");
                ManagementObjectSearcher mOSearcher = new ManagementObjectSearcher(sQQuery);
                _totalMemory = ((float.Parse(mOSearcher.Get().Cast<ManagementObject>().First().GetPropertyValue("TotalVisibleMemorySize").ToString())) / 1024);
            }
            catch
            {
                _totalMemory = 0;
            }


        }

        void refreshFreeMemory()
        {
            try
            {
                PerformanceCounter freeMem = new PerformanceCounter("Memory", "Available MBytes", true);
                _freeMemory = freeMem.NextValue();
            }
            catch
            {

                _freeMemory = 0;
            }
        }

        void refreshTotalCPU()
        {
            try
            {
                SelectQuery sQQuery = new SelectQuery("CIM_Processor");
                ManagementObjectSearcher mOSearcher = new ManagementObjectSearcher(sQQuery);
                ManagementObjectCollection mOCollection = mOSearcher.Get();
                _totalCPU = float.Parse(mOSearcher.Get().Cast<ManagementObject>().First().GetPropertyValue("CurrentClockSpeed").ToString());
            }
            catch
            {
                _totalCPU = 0;
            }

        }

        void refreshFreeCPU()
        {
            try
            {
                byte ITERATIONS = 3;
                SelectQuery sQQuery = new SelectQuery("CIM_Processor");
                ManagementObjectSearcher mOSearcher = new ManagementObjectSearcher(sQQuery);
                float fSummedValues = 0;
                for (int i = 0; i < ITERATIONS; i++)
                {
                    float fLoadPercentage = float.Parse(mOSearcher.Get().Cast<ManagementObject>().First().GetPropertyValue("LoadPercentage").ToString());
                    float fCurrentClockSpeed = float.Parse(mOSearcher.Get().Cast<ManagementObject>().First().GetPropertyValue("CurrentClockSpeed").ToString());
                    fSummedValues += fCurrentClockSpeed - (fLoadPercentage * fCurrentClockSpeed / 100);
                }

                _freeCPU = fSummedValues / ITERATIONS;
            }
            catch
            {
                _freeCPU = 0;

            }
        }

        #endregion
       
    
    }
}
